package com.angel.controller;

import com.angel.bean.CmsInfo;
import com.angel.bean.Employee;
import com.angel.dto.CmsInfoDto;
import com.angel.dtoutlis.CmsInfoDtoUtil;
import com.angel.exception.StatusException;
import com.angel.service.CmsInfoService;
import com.angel.service.EmployeeService;
import com.angel.service.OtherlogService;
import com.angel.shiro.SystemAdminAuthorizingRealm;
import com.angel.utils.AdminUserUtils;
import com.angel.utils.LogUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cmsController")
public class CmsController {

    @Resource
    private CmsInfoService cmsInfoService;

    @Resource
    private EmployeeService employeeService;
    @Autowired
    private OtherlogService otherlogService;



    /**
     * 访问CMS发布页
     *
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequiresPermissions("user")
    @RequestMapping("/getCmsInfoPage")
    public String getCmsInfoPage() {
        return "cmsInfoSend";
    }

    /**
     * 查询单篇文章
     *
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequestMapping("/showCmsInfoOne")
    public String showCmsInfoOne(CmsInfo cmsInfo, Model model) {

        //当前用户信息
        SystemAdminAuthorizingRealm.Principal principal = AdminUserUtils.getPrincipal();

        CmsInfo cmsInfoOne = cmsInfoService.findOne(cmsInfo);
        if(cmsInfoOne !=null){
            model.addAttribute("cmsInfoOne", cmsInfoOne);
            model.addAttribute("principal", principal);
        }
        return "cmsInfoDetails";
    }

    /**
     * 查询单篇文章
     *  请求
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequestMapping("/showCmsInfoOneEdit")
    public String showCmsInfoOneEdit(CmsInfo cmsInfo,Model model) {

        System.out.println("-------------------进入请求获得单篇文章控制层");
        CmsInfo cmsInfoOne = cmsInfoService.findOne(cmsInfo);
        model.addAttribute("cmsInfoOne", cmsInfoOne);
        return "cmsInfoUpdate";
    }

    /**
     * 添加新闻
     *
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequestMapping("/addCmsInfo")
    @ResponseBody
    public Map addCmsInfo(CmsInfo cmsInfo) {
        //存入当前用户id
        Map map = new HashMap();
        map.put("result",false);
        SystemAdminAuthorizingRealm.Principal principal = AdminUserUtils.getPrincipal();
        Employee employee = principal.getEmployee();
        String msg ="添加新闻信息";
        cmsInfo.setEditor(principal.getId());
        cmsInfo.setState(1);
        cmsInfo.setRelsDate(new Timestamp(System.currentTimeMillis()));
        int result = cmsInfoService.insert(cmsInfo);
        if (result != 0) {
            map.put("result",true);
        } else {
           map.put("msg","新闻添加失败");
        }
        LogUtils.InsertLog(employee,otherlogService,msg);
        return map;
    }

    /**
     * 更新新闻
     */
    @RequiresPermissions("user")
    @RequestMapping("/updateCmsInfo")
    @ResponseBody
    public Map updateCmsInfo(CmsInfo cmsInfo) {
        //存入当前用户id
        Map map = new HashMap();
        map.put("result",false);
        SystemAdminAuthorizingRealm.Principal principal = AdminUserUtils.getPrincipal();
        cmsInfo.setEditor(principal.getId());
        Employee employee = principal.getEmployee();
        String msg ="更新新闻信息";
        cmsInfo.setState(1);
        cmsInfo.setRelsDate(new Timestamp(System.currentTimeMillis()));
        int result = cmsInfoService.update(cmsInfo);
        if (result != 0) {
            map.put("result",true);
        } else {
          map.put("msg","更新失败");
        }
        LogUtils.InsertLog(employee,otherlogService,msg);
        return map;
    }

    /**
     * 删除单篇文章
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequestMapping("/deleteCmsInfo")
    @ResponseBody
    public Object deleteCmsInfo(CmsInfo cmsInfo) {
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        String msg ="删除新闻信息："+cmsInfo.getId();
        Map<Object, Object> map = new HashMap<Object, Object>();

        cmsInfo.setState(0);  // 逻辑删除
        int result = cmsInfoService.update(cmsInfo);
        if (result != 0) {
            msg+="成功";
            map.put("result", "success");
        }
        LogUtils.InsertLog(employee,otherlogService,msg);
        return map;
    }

    //分页实现
    /**
     * 查询用户信息
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequiresPermissions("user")
    @RequestMapping("/showCmsInfoPage")
    @ExceptionHandler(value = StatusException.class)
    public String showCmsInfoPage(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {

        int pageNo =1;
        try {
            pageNo =Integer.valueOf(pn);
        }catch (Exception e){

        }
        PageHelper.startPage(pageNo,5); //传入当前页
        CmsInfo cmsInfo = new CmsInfo();
        cmsInfo.setState(1);
        List<CmsInfo> cmsInfoList = cmsInfoService.findAll(cmsInfo);
        Employee employee = new Employee();
        employee.setState(1);
        List<Employee> employeeList = employeeService.findEmployee(employee);
        PageInfo pageInfo = new PageInfo(cmsInfoList);

        List<CmsInfoDto> dtoList = CmsInfoDtoUtil.getCmsInfoDto(pageInfo.getList(), employeeList);

        pageInfo.setList(dtoList);

        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "cmsInfoShow";
    }

}
